package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.adapter.MovieAdapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class AdvancedSearchActivity extends Activity
{
	EditText title;
	EditText director;
	EditText genre;
	EditText years;
	Button searchButton;
	LinearLayout searchList;
	
	final String EXTRA_MOVIE_NAME = "movie_name";
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.advanced_search_layout);
		
		title = (EditText)findViewById(R.id.titreEditText);
		director = (EditText)findViewById(R.id.directorEditText);
		genre = (EditText)findViewById(R.id.genreEditText);
		years = (EditText)findViewById(R.id.yearEditText);
		searchButton = (Button)findViewById(R.id.searchButton);
		searchList = (LinearLayout) findViewById(R.id.searchListView);
		
		searchButton.setOnClickListener(searchListener);
	}
	
	public OnClickListener searchListener = new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
        	searchList.removeAllViews();
    		String stringTitle = title.getText().toString();
    		String stringDirector = director.getText().toString();
    		String stringGenre = genre.getText().toString();
    		String stringYears = years.getText().toString();
    		
    		ArrayList<String> result  = MainActivity.mLibFilm.advancedSearch(stringTitle,stringDirector,stringGenre,stringYears);
    		
    		ArrayList<String> movieName = new ArrayList<String>();
            ArrayList<String> movieUrl = new ArrayList<String>();
    		
    		for(int i = 0; i < result.size(); i++)
    		{
    			for(int j = 0; j < MainActivity.mLibFilm.getListFilm().size(); j++)
	            {
    				if(result.get(i).equals(MainActivity.mLibFilm.getListFilm().get(j).getNom()))
    				{
    	    			movieName.add(MainActivity.mLibFilm.getListFilm().get(j).getNom());
    	    			movieUrl.add(MainActivity.mLibFilm.getListFilm().get(j).getImageUrl());
    				}
	            }
    		}
    		
    		MovieAdapter movieAdapter = new MovieAdapter(AdvancedSearchActivity.this, movieName, movieUrl);
    		
    		final int MovieCount = movieAdapter.getCount();
    		for (int i = 0; i < MovieCount; i++)
    		{
    			View item = movieAdapter.getView(i, null, null);
    			searchList.addView(item);
    		}
    		hideSoftKeyboard();
        }
    };
    
    private void hideSoftKeyboard()
    {
        if(getCurrentFocus()!=null && getCurrentFocus() instanceof EditText){
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }
}